package vip.liux.backend.infrastructure.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static vip.liux.backend.infrastructure.consts.MessageConst.*;

@Configuration(proxyBeanMethods = false)
public class RabbitMqConfig {

    @Bean
    public Queue orderQueue() {
        Map<String, Object> args = new HashMap<>(2);
        // 绑定我们的死信交换机
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        // 绑定我们的路由key
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 10000); // TTL: 单位:
        args.put("x-max-length", 1000);
        return new Queue(BUSINESS_QUEUE, true, false, false, args);
    }

    //绑定交换机
    @Bean
    public DirectExchange orderExchange() {
        return new DirectExchange(BUSINESS_EXCHANGE);
    }

    //绑定路由键
    @Bean
    public Binding orderBindingExchange(Queue orderQueue, DirectExchange orderExchange) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with(BUSINESS_ROUTING_KEY);
    }
}
